<template>
  <div class="login">
    <div class="loginBox">
      <div class="avtor">
        <img src="https://ftp.bmp.ovh/imgs/2021/05/684c59686a76f389.jpg" alt />
      </div>
      <form class="form" :model="value" ref="formRef" @submit.prevent="onSubmit">
        <div class="form-1">
          <label for>用户名</label>
          <input type="text" placeholder="请输入用户名" v-model="value.username" />
        </div>
        <div class="form-2">
          <label for>密码</label>
          <input type="password" placeholder="请输入密码" v-model="value.password" />
        </div>
        <div class="btn">
          <button style="backgroundColor:darkcyan" @click="login">登录</button>
          <button @click="register">注册</button>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import {initDynamicRouter} from '@/router'
export default {
  name: "login",
  props: [],
  components: {},
  data() {
    return {
      value: {
        username: "",
        password: ""
      }
    };
  },
  //方法 函数写这里
  methods: {
    onSubmit: function() {
      return false;
    },
    register() {
      this.$router.push("./register");
    },
    login() {
      if (this.value.username == "" || this.value.password == "") {
        return this.$message.error("登录失败 请重新输入!!!");
      }
      this.$http
        .post("http://127.0.0.1:3003/login", {
          params: this.value
        })
        .then(res => {
          console.log(res);
          this.$router.push("/home/shouye");
          if (res.data.code == 401) {
            return this.$message.error(res.data.msg);
          }
          // 这一步就存到了vuex里面全部返回数据
          this.$store.commit('setUser',res.data)
          //后台管理权限的动态路由的动态添加
          initDynamicRouter();
          

          sessionStorage.setItem("v-user", JSON.stringify(res.data));
          sessionStorage.setItem("token", res.data.username);

          this.$router.push("/home/shouye");
          // if (res.data.username === "霸道总裁") {
          // }
          // if (res.data.code != 200) {
          //   return this.$message.error("登录失败 请重新输入!!!");
          // }
          // this.$message.success("登陆成功");
          // window.sessionStorage.setItem("token", res.data.token);
          // this.$router.push("/home/shouye");
        });
    }
  },
  //计算属性
  computed: {},
  //侦听器
  watch: {},
  //过滤器
  filters: {},
  //以下是生命周期
  //组件创建之前
  beforeCreate() {},
  //组件创建之后
  created() {},
  //页面渲染之前
  beforeMount() {},
  //页面渲染之后
  mounted() {},
  //页面视图数据更新之前
  beforeUpdate() {},
  //页面视图数据更新之后
  updated() {},
  //页面销毁之前
  beforeDestroy() {},
  //页面销毁之后
  destroyed() {}
};
</script>
<style lang="scss" scoped>
.login {
  background-color: darkcyan;
  height: 100vh;
}
.loginBox {
  width: 450px;
  height: 350px;
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 5px;
}
.avtor {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  position: absolute;
  transform: translate(-15%, -15%);
  padding: 5px;
  box-shadow: 0 0 12px #dddddd;
  border: 1px solid #eee;
  background-color: #ffffff;
}
img {
  width: 60px;
  height: 60px;
  border-radius: 50%;
}
.form {
  position: absolute;
  width: 100%;
  height: 200px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 0 20px;
  box-sizing: border-box;
  div {
    margin: 30px 40px 0px 40px;
  }
}
label {
  display: inline-block;
  width: 60px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  font-size: 16px;
  margin-right: 10px;
}
input {
  width: 220px;
  height: 20px;
}
.btn {
  display: flex;
  justify-content: center;
}
button {
  width: 80px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  margin: 0px 20px;
}
</style>
